package com.kstudio.ksnapshot.view;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.kstudio.ksnapshot.KUtil;

public class KSeekView extends GLSurfaceView implements Renderer {
    private KUtil kLog = new KUtil("KSeekView");
    private Context kContext;
    private KSeekViewListener kL;
    
    private KDraw2D kStartTime;
    private KDraw2D kEndTime;

    private KDraw2D kLeftBar;
    private KDraw2D kMiddleBar;
    private KDraw2D kRightBar;

    private KDraw2D kControl;
    
    private int kWidth;
    private int kHeight;

    private int kSeekBarPaddingTop;
    private int kSeekBarPaddingDown;
    private int kSeekBarPaddingEdge;
    
    private int KSeekBarLeftEdgePos;
    private int KSeekBarRightEdgePos;
    private int kSeekBarWidth;
    private int KSeekBarBottom;
    
    private int KSeekControlBottom;
    private int KSeekControlTop;
    private int KSeekControlPadding;
    
    private int kTextSize;
    private int kImageBarEdge;
    private int kImageBarMiddle;
    private int kImageControlNormal;
    
    private int kDuration;
    
    public KSeekView(Context context) {
        this(context, null);
    }

    public KSeekView(Context context, AttributeSet attrs) {
        super(context, attrs);
        kLog.i("KSeekView");
        
        kContext = context;
        kL = null;
        
        kStartTime = null;
        kEndTime = null;;

        kLeftBar = null;
        kMiddleBar = null;
        kRightBar = null;

        kControl = null;
        
        kSeekBarPaddingTop = 0;
        kSeekBarPaddingDown = 0;
        kSeekBarPaddingEdge = 0;

        KSeekBarLeftEdgePos = 0;
        KSeekBarRightEdgePos = 0;
        kSeekBarWidth = 0;
        KSeekBarBottom = 0;
        
        KSeekControlBottom = 0;
        KSeekControlTop = 0;
        KSeekControlPadding = 0;
        
        kTextSize = 10;
        kDuration = 0;
                
        this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        this.setRenderer(this);
        this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        this.setZOrderMediaOverlay(true);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        kLog.i("onSurfaceCreated");

        gl.glDisable(GL10.GL_DITHER);

        gl.glClearColor(0,0,0,0);

        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glClearDepthf(1.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);

        gl.glEnable(GL10.GL_BLEND);
//        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL10.GL_ALPHA_TEST);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        kLog.i("onSurfaceChanged w = %d h = %d",width,height);
        kWidth = width;
        kHeight = height;

        gl.glViewport(0, 0, width,height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

//        gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
        gl.glOrthof(0, width, 0, height, -1, 1);

        KCreateElements(gl);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        kStartTime.draw(gl);
        kEndTime.draw(gl);

        kLeftBar.draw(gl);
        kMiddleBar.draw(gl);
        kRightBar.draw(gl);

        kControl.draw(gl);
        
        gl.glEnable(GL10.GL_BLEND);
//        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL10.GL_ALPHA_TEST);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        kLog.i("onTouchEvent %d", event.getAction());
        int action = event.getAction();
        int xPos = (int) event.getX();
        int yPos = kHeight - (int) event.getY();
        
        switch(action) {
            case MotionEvent.ACTION_DOWN : {
                kLog.i("onTouchEvent x = %d , y = %d",xPos,yPos);
                if(kControl.checkIncludePosition(xPos, yPos)) {
                    kLog.i("ACTION_DOWN TRUE");
                    return true;
                }
                break;
            }
            case MotionEvent.ACTION_MOVE : {
                kLog.i("ACTION_MOVE x = %f , y = %f",event.getX(),event.getY());
                kControl.move(xPos - KSeekControlPadding, 0);
                KSeekViewChangePostion(kControl.getCurrentPosX());
                return true;
            }
            case MotionEvent.ACTION_UP : 
            case MotionEvent.ACTION_CANCEL : {
                return true;
            }
        }
        return super.onTouchEvent(event);
    }

    private void KCreateElements(GL10 gl) {
        int l = 0;
        int t = 0;
        int r = 0;
        int b = 0;

        if(kStartTime == null) {
            kStartTime = new KDraw2D();
            kStartTime.loadTextureFromString(gl, kTextSize, "01:34");
            l = 0;
            r = l + kStartTime.getWidth();
            b = 0;
            t = KSeekControlBottom = b + kStartTime.getHeight();
            kStartTime.create(l, t, r, b);
        }
        
        if(kEndTime == null) {
            kEndTime = new KDraw2D();
            kEndTime.loadTextureFromString(gl, kTextSize, "56:78");
            l = kWidth - kEndTime.getWidth();
            r = l + kEndTime.getWidth();
            b = 0;
            t = KSeekControlBottom = b + kEndTime.getHeight();
            kEndTime.create(l, t, r, b);
        }
        
        if(kControl == null) {
            kControl = new KDraw2D();
            kControl.loadTextureFromResourecID(gl, kContext, kImageControlNormal);
            l = KSeekControlPadding = kSeekBarPaddingEdge - kControl.getWidth() / 2;
            r = l + kControl.getWidth();
            b = KSeekControlBottom;
            t = KSeekControlTop = b + kControl.getHeight();
            kLog.d("kControl h = %d", kControl.getHeight());
            kControl.create(l, t, r, b);
        }

        if(kLeftBar == null) {
            kLeftBar = new KDraw2D();
            kLeftBar.loadTextureFromResourecID(gl, kContext, kImageBarEdge);
            l = kSeekBarPaddingEdge;
            r = l + kWidth * 1 / 20;
            b = KSeekBarBottom = KSeekControlBottom + (kControl.getHeight() - kLeftBar.getHeight()) / 2;
            kLog.d("kLeftBar %d %d %d", KSeekBarBottom, kControl.getHeight(), kLeftBar.getHeight());
            t = b + kLeftBar.getHeight();
            kLeftBar.create(l, t, r, b);
        }

        if(kRightBar == null) {
            kRightBar = new KDraw2D();
            kRightBar.loadTextureFromResourecID(gl, kContext, kImageBarEdge);
            l = KSeekBarRightEdgePos = kWidth * 19 / 20  - kSeekBarPaddingEdge;
            r = l + kWidth * 1 / 20;
            b = KSeekBarBottom;
            t = b + kRightBar.getHeight();
            kRightBar.create(l, t, r, b);
        }

        if(kMiddleBar == null) {
            kMiddleBar = new KDraw2D();
            kMiddleBar.loadTextureFromResourecID(gl, kContext, kImageBarMiddle);
            l = kLeftBar.getRight();
            r = kRightBar.getLeft();
            b = KSeekBarBottom;
            t = b + kMiddleBar.getHeight();
            kMiddleBar.create(l, t, r, b);
        }
        
        KSeekBarLeftEdgePos = kLeftBar.getWidth();
        KSeekBarRightEdgePos = kLeftBar.getWidth() + kLeftBar.getWidth();
        kSeekBarWidth = kLeftBar.getWidth() + kMiddleBar.getWidth() + kLeftBar.getWidth();
        kControl.setMaxMoveXPos(kSeekBarWidth);
        kLog.d("kSeekBarWidth = %d", kSeekBarWidth); 
    }

    private void KSeekViewChangePostion(int pos){
        int time = (pos * kDuration ) / kSeekBarWidth;
        
        if(kL != null) kL.onKSeekViewChangeTime(time);
    }

    public void KSetTextSize(int size) {
        kTextSize = size;
    }

    public void KSetSeekBarEdge(int id) {
        kImageBarEdge = id;
    }

    public void KSetSeekBarMiddle(int id) {
        kImageBarMiddle = id;
    }

    public void KSetSeekControlNormal(int id) {
        kImageControlNormal = id;
    }

    public void KSetSeekBarPaddingEdge(int p) {
        kSeekBarPaddingEdge = p;
    }
    
    public void KSetSeekBarPaddingTop(int p) {
        kSeekBarPaddingTop = p;
    }
    
    public void KSetSeekBarPaddingDown(int p) {
        kSeekBarPaddingDown = p;
    }
    
    public void KSetDuration(int duration) {
        kLog.e("KSetDuration = %d", duration);
        kDuration = duration;
    }
    
    public void KSetSeekViewListener(KSeekViewListener l) {
        kL = l;
    }

    public interface KSeekViewListener {
        void onKSeekViewChangeTime(int time);
    }
}
